***********************************************************************************
********"Macroprudential Regulation, Quantitative Easing, and Bank Lending"********
***********************************************************************************
* This file prepares the initial information dataset and check if the loan request 
* has been accepted or rejected
***********************************************************************************
***********************************************************************************

*==================================================================================
**# Bookmark (input)
clear *
import sas ENTE_SEGN CENSITO_CRITTOG TIME DISBUR COM COMUNE_DIP using $inputdirectory/bloans2019long, case(lower)
run RO_creditregister tm(2018m12) tm(2020m5) 0

keep bankid borrowerid time disbur
collapse (sum) disbur, by(bankid borrowerid time)

merge m:1 bankid using $inputdirectory/INB_gru2019, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid
collapse (sum) disbur, by(bankid borrowerid time)

egen id=group(bankid borrowerid)

xtset id time

gen timem3=time+3
gen timem2=time+2
gen timem1=time+1
gen time1=time-1
gen time2=time-2
gen time3=time-3

drop disbur 
gen count=1

**# Bookmark (saving temporary dataset useful to check if the requested loan
* has been granted)
compress
save tempiis, replace

*==================================================================================
**# Bookmark (input)
clear *
import sas using $inputdirectory/iis2019, case(lower)

rename ente_segn bankid
rename censito_crittog borrowerid
rename max_a_sintetica_ short
rename max_a_ndate_rich_ nmonth
format borrowerid %20.0g

tostring(time), replace
gen yyy=date(time, "YMD")
drop time
gen time=mofd(yyy)
drop yyy
format time %tm

keep if time>=tm(2019m3)&time<=tm(2020m2)

merge m:1 bankid using temp
keep if _merge==3
drop _merge

merge m:1 bankid using $inputdirectory/INB_gru2019, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid
collapse (max) short nmonth, by(bankid borrowerid time)

gen timem3=time
gen timem2=time
gen timem1=time
gen time1=time
gen time2=time
gen time3=time

merge 1:1 bankid borrowerid time using tempiis, keepus(count)
drop count
rename _merge t0
keep if t0!=2

merge 1:1 bankid borrowerid time1  using tempiis, keepus(count)
drop count
rename _merge t1
keep if t1!=2

merge 1:1 bankid borrowerid time2 using tempiis, keepus(count)
drop count
rename _merge t2
keep if t2!=2

merge 1:1 bankid borrowerid time3 using tempiis, keepus(count)
drop count
rename _merge t3
keep if t3!=2

merge 1:1 bankid borrowerid timem1 using tempiis, keepus(count)
drop count
rename _merge tm1
keep if tm1!=2

merge 1:1 bankid borrowerid timem2 using tempiis, keepus(count)
drop count
rename _merge tm2
keep if tm2!=2

merge 1:1 bankid borrowerid timem3 using tempiis, keepus(count)
drop count
rename _merge tm3
keep if tm3!=2

recode t0 (1=0) (3=1)
recode t1 (1=0) (3=1)
recode t2 (1=0) (3=1)
recode t3 (1=0) (3=1)
recode tm1 (1=0) (3=1)
recode tm2 (1=0) (3=1)
recode tm3 (1=0) (3=1)

keep if tm1==0&tm2==0&tm3==0
replace t1=1 if t0==1
replace t2=1 if t1==1
replace t3=1 if t2==1

drop time1 time2 time3
rename time date

collapse (max) t*, by(borrowerid bankid date)
rename date time
sort borrowerid time 

egen t0123=rowmax(t0 t1 t2 t3)

**# Bookmark (saving dataset - loan requests and outcome)
compress
save $inputdirectory/INB_extensive2019, replace

erase tempiis.dta
erase temp.dta
